package com.example.fastdfs_demo.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;
import java.util.Base64.Decoder;

public class Base64StrToImage {

	 Logger logger = LoggerFactory.getLogger(Base64StrToImage.class);
     public  static MultipartFile base64MutipartFile(String imgStr){
         try {
             String [] baseStr = imgStr.split(",");
             //BASE64Decoder base64Decoder = new BASE64Decoder();
             Decoder base64Decoder = Base64.getDecoder();
             byte[] b =  new byte[0];
             b = base64Decoder.decode(baseStr[1]);
             for(int i = 0; i < b.length; ++i) {
                 if (b[i] < 0) {
                     b[i] += 256;
                 }
             }
             return  new BASE64DecodedMultipartFile(b,baseStr[0]) ;
         }catch (Exception e){
             e.printStackTrace();
             return null;
         }
     }
}
